package com.ptmall.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.TimeSelectBean;
import com.ptmall.app.ui.adapter.TimeAdapter;
import com.ptmall.app.view.TitleBarView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by Administrator on 2019/4/1.
 */

public class SelectTimeActivity extends BaseMvpActivity {
    private static String mYear;
    private static String mMonth;
    private static String mDay;
    private static String mWay;
    ListView listView;

    TimeAdapter timeAdapter;
    TitleBarView titlebar;

    List<TimeSelectBean> list = new ArrayList<>();
    Button enter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_selecttime);
    }

    @Override
    public void initViews() {
        list.add(new TimeSelectBean());
        list.add(new TimeSelectBean());
        list.add(new TimeSelectBean());
        listView = (ListView) findViewById(R.id.listview);
        enter = (Button) findViewById(R.id.enter);
        titlebar = (TitleBarView) findViewById(R.id.titlebar);
        timeAdapter = new TimeAdapter();
        listView.setAdapter(timeAdapter);
        timeAdapter.pushData(list);
        timeAdapter.setClick(new TimeAdapter.Click() {
            @Override
            public void Click(boolean position, int i) {
                for (TimeSelectBean timeSelectBean : list) {
                    timeSelectBean.time1 = false;
                    timeSelectBean.time2 = false;
                }
                list.get(i).time1 = true;
                timeAdapter.pushData(list);
            }

            @Override
            public void Click2(boolean position, int i) {
                for (TimeSelectBean timeSelectBean : list) {
                    timeSelectBean.time1 = false;
                    timeSelectBean.time2 = false;
                }
                list.get(i).time2 = true;
                timeAdapter.pushData(list);
            }
        });
        titlebar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
    }

    @Override
    public void initEvents() {
        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).time1) {
                        intent.putExtra("pei_date",time1(i+1));
                        intent.putExtra("pei_time","9:30-12:00");
                    }
                    if (list.get(i).time2) {
                        intent.putExtra("pei_date",time1(i+1));
                        intent.putExtra("pei_time","15:30-17:00");
                    }
                }
                setResult(3,intent);
                finish();
            }
        });
    }


    public String time2(int i){
        final Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, i);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if("1".equals(mWay)){
            mWay ="日";
        }else if("2".equals(mWay)){
            mWay ="一";
        }else if("3".equals(mWay)){
            mWay ="二";
        }else if("4".equals(mWay)){
            mWay ="三";
        }else if("5".equals(mWay)){
            mWay ="四";
        }else if("6".equals(mWay)){
            mWay ="五";
        }else if("7".equals(mWay)){
            mWay ="六";
        }
        return "周"+mWay;
    }
    public String time1(int i){
        final Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, i);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if("1".equals(mWay)){
            mWay ="日";
        }else if("2".equals(mWay)){
            mWay ="一";
        }else if("3".equals(mWay)){
            mWay ="二";
        }else if("4".equals(mWay)){
            mWay ="三";
        }else if("5".equals(mWay)){
            mWay ="四";
        }else if("6".equals(mWay)){
            mWay ="五";
        }else if("7".equals(mWay)){
            mWay ="六";
        }
        if(new BigDecimal(mMonth).doubleValue()<9){
            return  mYear+"-0"+mMonth + "-" + mDay;
        }
        return  mYear+"-"+mMonth + "-" + mDay;
    }
}
